using System;
using System.Collections.Generic;
using System.Text;

namespace PokerLearner.Poker
{
	public enum CardSuite : byte
	{
		Diamonds,
		Clubs,
		Hearts,
		Spades
	}

	[Serializable]
	public class Card : IComparable<Card>
	{
		public byte Count
		{
			get { return (byte)((value % 13) + 1); }
		}

		private byte value;

		public byte Value
		{
			get { return value; }
		}

		public Card(byte value)
		{
			this.value = value;
		}

		public CardSuite Suite
		{
			get { return (CardSuite)(this.value / 13); }
		}

		#region IComparable<Card> Members

		public int CompareTo(Card other)
		{
			if (this.Count < other.Count)
				return -1;
			else if (this.Count > other.Count)
				return 1;
			else
				return 0;
		}

		#endregion

		public override string ToString()
		{
			return ToString(false);
		}

		public string ToString(bool abbreviate)
		{
			StringBuilder builder = new StringBuilder();
			if(abbreviate)
				builder.AppendFormat("{0}{1}", (Count + 1), Suite.ToString().Substring(0, 1));
			else
				builder.AppendFormat("{0} {1}", (Count + 1), Suite);

			return builder.ToString();
		}
	}

	public class Deck
	{
		#region Private Fields

		private List<Card> cards;
		Random random = new Random();

		#endregion

		#region Shuffle

		public void Shuffle()
		{
			cards.Clear();
			for (byte i = 0; i < 52; i++)
			{
				cards.Add(new Card(i));
			}
		}

		#endregion

		#region GetCard

		public Card GetCard()
		{						
			Card ret = cards[random.Next(cards.Count)];
			cards.Remove(ret);
			return ret;
		}

		#endregion

		#region Constructor

		public Deck()
		{
			cards = new List<Card>();
			random = new Random();
			Shuffle();
		}

		#endregion
	}
}
